<template>
  <div class="page-container">
    <!--工具栏-->
    <div class="toolBar" style="float:left;padding-top: 10px;padding-left: 50px;">
      <el-form :inline="true" :model="filters" :size="size" ref="filters">
        <el-form-item prop="tradeMonth">
          <el-date-picker
            v-model="filters.tradeMonth"
            value-format="yyyy-MM"
            type="month"
            placeholder="统计月份"
            :picker-options="pickerOptions"
            style="width: 10em">
          </el-date-picker>
        </el-form-item>
        <el-form-item>
          <kt-button type="primary"
                     icon="fa fa-search"
                     :label="$t('action.search')"
                     perms="finance:fixed:view"
                     @click="findPage"/>
        </el-form-item>
        <el-form-item>
          <kt-button type="primary"
                     icon="fa fa-refresh"
                     :label="$t('action.reset')"
                     perms="finance:fixed:view"
                     @click="reset"/>
        </el-form-item>
        <el-form-item>
          <kt-button type="primary"
                     icon="fa fa-plus"
                     :label="$t('action.add')"
                     perms="finance:fixed:add"
                     @click="showAdd"/>
        </el-form-item>
        <el-form-item>
          <kt-button type="primary"
                     icon="fa fa-filter"
                     label="列显示"
                     perms="finance:fixed:view"
                     @click="displayFilterColumnDialog"/>
        </el-form-item>
      </el-form>
      <!--表格显示列界面-->
      <table-column-filter-dialog ref="tableColumnFilterDialog" :columns="columns" @handleFilterColumns="handleFilterColumns">
      </table-column-filter-dialog>
    </div>

    <!--表格内容栏-->
    <kt-table perms-edit="sale:goods:edit" :data="pageResult" :columns="filterColumns"
              :exec-summary="true" @findPage="findPage" @handleEdit="showEdit">
    </kt-table>

    <!--新增编辑界面-->
    <el-dialog :title="operation?'新增':'编辑'" width="30%" :visible.sync="dialogVisible" :close-on-click-modal="false">
      <el-form :model="dataForm" label-width="100px" :rules="dataFormRules"
               ref="dataForm" :size="size" label-position="right">
        <el-form-item label="ID" prop="id" v-if="false">
          <el-input v-model="dataForm.id" :disabled="true" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="统计月份" prop="tradeMonth">
          <el-date-picker v-model="dataForm.tradeMonth" placeholder="选择月份" type="month"></el-date-picker>
        </el-form-item>
        <el-form-item label="美工费用(￥)" prop="artFee">
          <el-input-number v-model="dataForm.artFee"
                           size="large"
                           :precision="2"
                           :max='0.00'
                           :step='50'>
          </el-input-number>
        </el-form-item>
        <el-form-item label="样品费用(￥)" prop="sampleFee">
          <el-input-number v-model="dataForm.sampleFee"
                           size="large"
                           :precision="2"
                           :max='0.00'
                           :step='50'>
          </el-input-number>
        </el-form-item>
        <el-form-item label="耗材费用(￥)" prop="packMaterialFee">
          <el-input-number v-model="dataForm.packMaterialFee"
                           size="large"
                           :precision="2"
                           :max='0.00'
                           :step='50'
                           align="left">
          </el-input-number>
        </el-form-item>
        <el-form-item label="人工费用(￥)" prop="laborFee">
          <el-input-number v-model="dataForm.laborFee"
                           size="large"
                           :precision="2"
                           :max='0.00'
                           :step='50'>
          </el-input-number>
        </el-form-item>
        <el-form-item label="租金水电(￥)" prop="officeRentFee">
          <el-input-number v-model="dataForm.officeRentFee"
                           size="large"
                           :precision="2"
                           :max='0.00'
                           :step='50'>
          </el-input-number>
        </el-form-item>
        <el-form-item label="财务记账(￥)" prop="accountingFee">
          <el-input-number v-model="dataForm.accountingFee"
                           size="large"
                           :precision="2"
                           :max='0.00'
                           :step='50'>
          </el-input-number>
        </el-form-item>
        <el-form-item label="头程费用(含send)(￥)" prop="headwayFee">
          <el-input-number v-model="dataForm.headwayFee"
                           size="large"
                           :precision="2"
                           :max='0.00'
                           :step='50'>
          </el-input-number>
        </el-form-item>
        <el-form-item label="其他费用(￥)" prop="otherFee">
          <el-input-number v-model="dataForm.otherFee"
                           size="large"
                           :precision="2"
                           :max='0.00'
                           :step='50'>
          </el-input-number>
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input type="textarea"
                    autosize
                    :show-word-limit="true"
                    v-model="dataForm.remark"
                    maxlength="100">
          </el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer" align="center">
        <el-button :size="size" @click.native="dialogVisible=false">{{$t('action.cancel')}}</el-button>
        <el-button :size="size" type="primary" :loading="editLoading" @click.native="submitForm">
          {{$t('action.submit')}}
        </el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
  import KtTable from '../core/KtTable'
  import KtButton from '../core/KtButton'
  import TableColumnFilterDialog from '../core/TableColumnFilterDialog'

  export default {
    name: "Fixed",
    components: {
      KtTable,
      KtButton,
      TableColumnFilterDialog
    },
    data() {
      return {
        size: 'small',
        filters: {
          tradeMonth: ''
        },
        //初始表格列显示字段
        columns: [],
        //列显示过滤字段
        filterColumns: [],
        pageRequest: {},
        pageResult: {},
        //true新增 false编辑
        operation: false,
        //新增编辑界面是否显示
        dialogVisible: false,
        editLoading: false,
        //新增或编辑页面
        dataForm: {
          id: 0,
          tradeMonth: '',
          artFee: 0.00,
          sampleFee: -1000.00,
          packMaterialFee: -1000.00,
          laborFee: -12000.00,
          officeRentFee: -4000.00,
          accountingFee: -450.00,
          headwayFee: 0.00,
          otherFee: 0.00,
          remark: ''
        },
        dataFormRules: {
          tradeMonth: [
            {required: true, message: '请选择月份', trigger: 'blur'}
          ]
        },
        pickerOptions: {
          shortcuts: [{
            text: '本月',
            onClick(picker) {
              picker.$emit('pick', new Date());
            }
          }, {
            text: '上个月',
            onClick(picker) {
              const start = new Date();
              start.setMonth(start.getMonth() - 1);
              picker.$emit('pick', start);
            }
          }, {
            text: '3个月前',
            onClick(picker) {
              const start = new Date();
              start.setMonth(start.getMonth() - 3);
              picker.$emit('pick', start);
            }
          }, {
            text: '6个月前',
            onClick(picker) {
              const start = new Date();
              start.setMonth(start.getMonth() - 6);
              picker.$emit('pick', start);
            }
          }]
        }
      }
    },
    methods: {
      // 处理表格列过滤显示
      initColumns() {
        this.columns = [
          {prop: "tradeMonth", label: "月份", minWidth: 100},
          {prop: "totalFee", label: "成本总计(￥)", minWidth: 100, formatter: true},
          {prop: "artFee", label: "美工(￥)", minWidth: 100, formatter: true},
          {prop: "sampleFee", label: "样品(￥)", minWidth: 100, formatter: true},
          {prop: "packMaterialFee", label: "耗材(￥)", minWidth: 100, formatter: true},
          {prop: "laborFee", label: "人工费用(￥)", minWidth: 100, formatter: true},
          {prop: "officeRentFee", label: "租金水电(￥)", minWidth: 120, formatter: true},
          {prop: "accountingFee", label: "财务记账(￥)", minWidth: 120, formatter: true},
          {prop: "headwayFee", label: "头程成本(￥)", minWidth: 100, formatter: true},
          {prop: "otherFee", label: "其他成本(￥)", minWidth: 100, formatter: true},
          {prop: "remark", label: "备注说明", minWidth: 150, showOverflow: true},
          {prop: "lastUpdator", label: "最近操作人", minWidth: 100},
          {prop: "lastUpdateTime", label: "最近操作时间", minWidth: 135}
        ];
        this.filterColumns = JSON.parse(JSON.stringify(this.columns));
      },
      //处理表格列过滤显示
      displayFilterColumnDialog() {
        this.$refs.tableColumnFilterDialog.setDialogVisible(true);
      },
      // 处理表格列过滤显示
      handleFilterColumns(data) {
        this.filterColumns = data.filterColumns;
        this.$refs.tableColumnFilterDialog.setDialogVisible(false);
      },
      //获取分页数据
      findPage(data) {
        if ((data !== null && typeof data !== 'undefined') && typeof data.pageRequest !== 'undefined') {
          this.pageRequest = data.pageRequest;
        }
        this.pageRequest.params = {
          tradeMonth: this.filters.tradeMonth
        };
        this.$api.fixedCost.findPage(this.pageRequest).then((res) => {
          if (res.code === 200) {
            this.pageResult = res.data;
          }
        }).then((data !== null && typeof data !== 'undefined') ? data.callback : '');
      },
      //重置搜索条件
      reset() {
        this.$refs['filters'].resetFields();
        this.findPage();
      },
      //显示新增界面
      showAdd() {
        this.dialogVisible = true;
        this.operation = true;
      },
      //显示编辑界面
      showEdit(params) {
        this.dialogVisible = true;
        this.operation = false;
        this.dataForm = Object.assign({}, params.row);
      },
      //提交表单
      submitForm() {
        this.$refs.dataForm.validate((valid) => {
          if (valid) {
            this.$confirm('确认提交吗?', '提示', {}).then(() => {
              this.editLoading = true;
              this.dataForm.tradeMonth = this.$moment(this.dataForm.tradeMonth).format('yyyy-MM');
              this.$api.fixedCost.save(this.dataForm).then((res) => {
                this.editLoading = false;
                if (res.code === 200) {
                  this.$message({message: '操作成功', type: 'success'});
                  this.dialogVisible = false;
                  this.$refs['dataForm'].resetFields();
                } else {
                  this.$message({message: res.message, type: 'error'});
                }
                this.findPage();
              });
            })
          }
        })
      },
    },
    mounted() {
      this.initColumns();
    }
  }
</script>

<style scoped>

</style>
